package logger

import (
	"fmt"
	"os"
	"strings"

	"github.com/sirupsen/logrus"
)

// 定义 ANSI 颜色代码
const (
	colorReset   = "\033[0m"  // 重置颜色
	colorInfo    = "\033[32m" // 绿色
	colorWarning = "\033[33m" // 黄色
	colorError   = "\033[31m" // 红色
	colorDebug   = "\033[34m" // 蓝色
)

type CustomFormatter struct {
}

func (f *CustomFormatter) Format(entry *logrus.Entry) ([]byte, error) {

	var logLine string
	level := strings.ToUpper(entry.Level.String()[:4])

	// var fName string
	// var funName string
	// var line int

	// if entry.HasCaller() {
	// 	fName = filepath.Base(entry.Caller.File)
	// 	funName = entry.Caller.Function
	// 	line = entry.Caller.Line
	// }

	if entry.Logger.Out == os.Stdout || entry.Logger.Out == os.Stderr {
		switch entry.Level {
		case logrus.InfoLevel:
			logLine = fmt.Sprintf("%s%s%s[%s] %s%s%s\n", colorInfo, level, colorReset, entry.Time.Format("2006-01-02 15:04:05"), colorInfo, entry.Message, colorReset)
		case logrus.WarnLevel:
			logLine = fmt.Sprintf("%s%s%s[%s] %s%s%s\n", colorWarning, level, colorReset, entry.Time.Format("2006-01-02 15:04:05"), colorWarning, entry.Message, colorReset)
		case logrus.ErrorLevel:
			logLine = fmt.Sprintf("%s%s%s[%s] %s%s%s\n", colorError, level, colorReset, entry.Time.Format("2006-01-02 15:04:05"), colorError, entry.Message, colorReset)
		case logrus.DebugLevel:
			logLine = fmt.Sprintf("%s%s%s[%s] %s%s%s\n", colorDebug, level, colorReset, entry.Time.Format("2006-01-02 15:04:05"), colorDebug, entry.Message, colorReset)
		default:
			logLine = fmt.Sprintf("%s[%s] %s\n", level, entry.Time.Format("2006-01-02 15:04:05"), entry.Message)
		}
	} else {
		logLine = fmt.Sprintf("%s[%s] %s\n", level, entry.Time.Format("2006-01-02 15:04:05"), entry.Message)
	}
	return []byte(logLine), nil
}
